<!-- Begin: widget - Search Refine -->
<div class="widget search-refine">
    <h1>Refinar Busca</h1>
    <ul>
        <?
        if (isset($type) && !isset(SearchResult::$params['brand'])):
            ?>
            <li>
                <h2>Marca</h2>
                <? foreach (DbGet::vehicle_countval('b.`name`', SearchResult::$params) as $value): ?>
                    <p>
                        <a href="<?= __BASE_URI__ . SearchResult::$url . 'marca.' . urlencode(strtolower($value->b_name)) . '/pagina.' . SearchResult::$page ?>"><?= $value->b_name ?></a>
                        <span>(<?= $value->count ?>)</span>
                    </p>
                <? endforeach ?>
            </li>
            <?
        endif;
        if (isset($brand) && !isset(SearchResult::$params['model'])):
            ?>
            <li>
                <h2>Modelo</h2>
                <? foreach (DbGet::vehicle_countval('m.`name`', SearchResult::$params) as $value): ?>
                    <p>
                        <a href="<?= __BASE_URI__ . SearchResult::$url . 'modelo.' . urlencode(strtolower($value->m_name)) . '/pagina.' . SearchResult::$page ?>"><?= $value->m_name ?></a>
                        <span>(<?= $value->count ?>)</span>
                    </p>
                <? endforeach ?>
            </li>
            <?
        endif;

        $data1 = DbGet::vehicle_countval('ve.`name`', SearchResult::$params);
        if (isset($model) && !isset(SearchResult::$params['version'])):
            if (isset($data1)) {
                ?>
                <li>
                    <h2>Versão</h2>
                    <? foreach ($data1 as $value): ?>
                        <p>
                            <a href="<?= __BASE_URI__ . SearchResult::$url . 'versao.' . urlencode(strtolower($value->ve_name)) . '/pagina.' . SearchResult::$page ?>"><?= $value->ve_name ?></a>
                            <span>(<?= $value->count ?>)</span>
                        </p>
                    <? endforeach ?>
                </li>
                <?
            }
        endif;


        if (!isset(SearchResult::$params['initialyear'])):
            ?>
            <li>
                <h2>A partir do ano</h2>
                <? foreach (DbGet::vehicle_countval('`firstreg`', SearchResult::$params) as $value): ?>
                    <p>
                        <a href="<?= __BASE_URI__ . SearchResult::$url . 'ano-inicial.' . urlencode($value->firstreg) . '/pagina.' . SearchResult::$page ?>"><?= $value->firstreg ?></a>
                        <span>(<?= $value->count ?>)</span>
                    </p>
                <? endforeach ?>
            </li>
            <?
        endif;
        if (!isset(SearchResult::$params['finalyear'])):
            ?>
            <li>
                <h2>Até o ano</h2>
                <? foreach (DbGet::vehicle_countval('`firstreg`', SearchResult::$params) as $value): ?>
                    <p>
                        <a href="<?= __BASE_URI__ . SearchResult::$url . 'ano-final.' . urlencode($value->firstreg) . '/pagina.' . SearchResult::$page ?>"><?= $value->firstreg ?></a>
                        <span>(<?= $value->count ?>)</span>
                    </p>
                <? endforeach ?>
            </li>
            <?
        endif;
        if (!isset(SearchResult::$params['advertiser'])):
            ?>
            <li>
                <h2>Oferecido por</h2>
                <p><a href="<?= __BASE_URI__ . SearchResult::$url . 'anunciante.revenda/pagina.' . SearchResult::$page ?>">Revenda</a></p>
                <p><a href="<?= __BASE_URI__ . SearchResult::$url . 'anunciante.particular/pagina.' . SearchResult::$page ?>">Particular</a></p>
            </li>
            <?
        endif;
        if (!isset(SearchResult::$params['city'])):
            ?>
            <li>
                <h2>Cidade</h2>
                <?
                $cities = array();
                foreach (DbGet::vehicle_countval('ctoc.`name`', SearchResult::$params) as $value) {
                    if (!empty($value->ctoc_name)) {
                        $obj = new stdClass();
                        $obj->name = $value->ctoc_name;
                        $obj->count = $value->count;
                        $cities[] = $obj;
                    }
                }
                foreach (DbGet::vehicle_countval('ctoe.`name`', SearchResult::$params) as $value) {
                    if (!empty($value->ctoe_name)) {
                        $exists = false;
                        foreach ($cities as $key => $city) {
                            if ($city->name == $value->ctoe_name) {
                                $cities[$key]->count += $value->count;
                                $exists = true;
                                break;
                            }
                        }
                        if (!$exists) {
                            $obj = new stdClass();
                            $obj->name = $value->ctoe_name;
                            $obj->count = $value->count;
                            $cities[] = $obj;
                        }
                        else
                            $exists = false;
                    }
                }
                foreach ($cities as $value):
                    ?>
                    <p>
                        <a href="<?= __BASE_URI__ . SearchResult::$url . 'cidade.' . urlencode(strtolower($value->name)) . '/pagina.' . SearchResult::$page ?>"><?= $value->name ?></a>
                        <span>(<?= $value->count ?>)</span>
                    </p>
                <? endforeach ?>
            </li>
        <? endif ?>
    </ul>
</div> <!-- End -->
